package com.cdtu.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.cdtu.entity.SysContract;
import com.cdtu.entity.SysDept;
import com.cdtu.entity.SysEmployee;
import com.cdtu.service.SysDeptService;
import com.cdtu.service.SysEmployeeService;
import org.apache.ibatis.annotations.Param;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Collections;
import java.util.Date;
import java.util.List;

/**
 * 类描述：
 *
 * @author xh
 * @date 2023-08-07 10:23
 **/
@Controller
@RequestMapping("/sysEmployeeController")
public class SysEmployeeControllerGzl {
    @Resource
    public SysEmployeeService sysEmployeeService;
    @Resource
    private SysDeptService sysDeptService;

    @RequestMapping("/list")
    public String list(HttpServletRequest request, HttpServletResponse response, Model model) {
        List<SysEmployee> sysEmployeeList = sysEmployeeService.list();
       model.addAttribute("sysEmployeeList",sysEmployeeList);
        List<SysDept> sysDeptList = sysDeptService.list();
       model.addAttribute("sysDeptList",sysDeptList);
        return "/system/sysEmployeeGzl/employeeFileList";
    }

    @RequestMapping("/showDept")
    public  String showDept(Model model)
    {
        List<SysDept> sysDeptList = sysDeptService.list();
        model.addAttribute("sysDeptList",sysDeptList);
        return "/system/sysEmployeeGzl/addSysEmployee";
    }

    @RequestMapping("/addSysEmloy")
    public String addSysEmloyee( SysEmployee sysEmployee,String entryDateStr,Model model) throws ParseException {
        Date date = new SimpleDateFormat("yy-MM-dd'T'HH:mm").parse(entryDateStr);
        sysEmployee.setEntryDate(date);
        sysEmployee.setCreated(new Date());
        sysEmployee.setEmpNo("a"+sysEmployee.getPhone());
        boolean save = sysEmployeeService.save(sysEmployee);
        return "redirect:/sysEmployeeController/list";

       /* List<SysEmployee> sysEmployeeList = sysEmployeeService.list();
        model.addAttribute("sysEmployeeList",sysEmployeeList);
        List<SysDept> sysDeptList = sysDeptService.list();
        model.addAttribute("sysDeptList",sysDeptList);
        return "/system/sysEmployeeGzl/employeeFileList";*/
    }


    @RequestMapping("delete")
    public String deleteSysEmloyee(SysEmployee sysEmployee){
       Long employeeId = Long.valueOf(sysEmployee.getId());
        sysEmployeeService.removeById(employeeId);
        return "redirect:/sysEmployeeController/list";
    }


    /*@RequestMapping("/updateByIdShow/{empId}")
    public String updateByIdShow(@PathVariable("empId") Long empId,Model model) {
        SysEmployee sysEmployee = sysEmployeeService.getById(empId);
        List<SysDept> sysDeptList = sysDeptService.list();
        model.addAttribute("sysDeptList",sysDeptList);
        model.addAttribute("sysEmployee",sysEmployee);
        return "/system/sysEmployeeGzl/updateSysEmployee";
    }*/
    @RequestMapping("/updateByIdShow")
    public String updateByIdShow(SysEmployee employee,Model model) {
        Long employeeId = Long.valueOf(employee.getId());
        SysEmployee sysEmployee = sysEmployeeService.getById(employeeId);
        List<SysDept> sysDeptList = sysDeptService.list();
        model.addAttribute("sysDeptList",sysDeptList);
        model.addAttribute("sysEmployee",sysEmployee);
        return "/system/sysEmployeeGzl/updateSysEmployee";
    }


    @RequestMapping("/update")
    public String updateById(SysEmployee sysEmployee,String entryDateStr) throws ParseException {
        Date date = new SimpleDateFormat("yy-MM-dd'T'HH:mm").parse(entryDateStr);
        sysEmployee.setEntryDate(date);
        sysEmployee.setEmpNo("a"+sysEmployee.getPhone());
        boolean b = sysEmployeeService.updateById(sysEmployee);
        return "redirect:/sysEmployeeController/list";
    }

    //批量删除
    @RequestMapping("deleteMulti")
    public String deleteManey(HttpServletRequest request){
        String[] idStr = request.getParameterValues("ids");
        System.out.println("=================================="+idStr);
        int[] id = new int[idStr.length];
        for (int i = 0; i < idStr.length; i++) {
            id[i] = Integer.parseInt(idStr[i]);
            boolean m =   sysEmployeeService.removeById(id[i]);
        }
        return "redirect:/sysEmployeeController/list";
    }

    @RequestMapping("/like")
    public String likeQuery(Model model, String nameStr)
    {
        System.out.println(nameStr);
        List<SysDept> list = sysDeptService.queryDeptByName("%"+nameStr+"%");
        Long[] deptIds = new Long[list.size()];
        int i = 0;
        for (SysDept sysDept : list) {
            deptIds[i]=sysDept.getDeptId();
            i++;
        }
        for (int i1 = 0; i1 < deptIds.length; i1++) {
            List<SysEmployee> sysEmployeeList = sysEmployeeService.listByIds(Collections.singleton(deptIds[i]));
            model.addAttribute("sysEmployeeList",sysEmployeeList);
        }
        /*for (SysDept sysDept : list) {
            System.out.println("==================="+sysDept.getDeptId());
        }*/

       /* model.addAttribute("sysEmployeeList",sysEmployeeList);*/
        return "redirect:/sysEmployeeController/list";

    }
}


